// 云函数入口文件
const cloud = require('wx-server-sdk')
const Core = require('@alicloud/pop-core');

exports.main = async (event, context) => {
  var client = new Core({
    accessKeyId: 'LTAI5tKw5zBwPwBGDLG7k6oC',
    accessKeySecret: 'CXcki4MThr6t2XFHUVgv0jLcsDnyqu',
    endpoint: 'https://dysmsapi.aliyuncs.com',
    apiVersion: '2017-05-25'
  });

  console.log(2);

  var params = {
    "RegionId": "cn-hangzhou",
    "phoneNumbers": event.phone,
    "signName": "理型公园",
    "templateCode": "SMS_468935309",
    // "templateParam": event.list
    "templateParam": "{\"lw\":\""+event.lw+"\",\"jk\":\""+event.jk+"\",\"xw\":\""+event.xw+"\",\"yw\":\""+event.yw+"\",\"xc\":\""+event.xc+"\",\"yc\":\""+event.yc+"\",\"bw\":\""+event.bw+"\",\"kc\":\""+event.kc+"\",\"dc\":\""+event.dc+"\",\"dtw\":\""+event.dtw+"\",\"xtw\":\""+event.xtw+"\",\"xfyc\":\""+event.xfyc+"\",\"xfxw\":\""+event.xfxw+"\",\"xfjk\":\""+event.xfjk+"\",\"xfxc\":\""+event.xfxc+"\",\"xkc\":\""+event.xkc+"\",\"xkyw\":\""+event.xkyw+"\",\"xktw\":\""+event.xktw+"\",\"cy\":\""+event.cy+"\"}"
  }
  console.log(params);
  var requestOption = {
    method: 'POST',
    header: {
        "Content-Type": "application/json"
    },
  };
  console.log(4);
  return new Promise((resolve,reject)=>{
    client.request('SendSms', params, requestOption, function (err, res) {
        try{
          console.log(res)
            resolve(res)
        }catch(err){
          console.log(err)
            reject(err)
        }
    })
})
  return await client.request('SendSms', params, requestOption);
}